# GRAPHING WITH DERIVATIVES
# This is the graphing example done in class. The commands find the
# roots of f(x) and its derivative..
> f:=x->x^4+4*x^3-2*x^2-12*x;
> solve(f(x)=0,x);
> evalf(");
> plot(f(x),x=-5..3);
> plot(f(x),x=-4..2);
> diff(f(x),x);
> g:=unapply(",x);
> solve(g(x)=0,x);
# I do not like the next plot but the idea is to show that the solutions
# of  g(x) = 0 coincide with the extreme points of f(x).
> plot([f(x),g(x)],x=-4..2);
> h:=unapply(diff(g(x),x),x);
> solve(h(x)=0,x);
> plot(h(x),x=-4..2);
# You have now found the inflexion points of f(x) and the last plot
# shows that the curvature of f is determined by the sign of h(x) 
> plot([f(x),h(x)],x=-4..2,y=-20..20);
# PROFIT MAXIMIZATION BY  A MONOPOLIST
> AR:=x->10-x+15*exp(-0.1*x);
> solve(AR(x)=0,x);
> plot(AR(x),x=0..14);
> TR:=x->x*AR(x);
> TR(5);
> plot(TR(x),x=0..14);
> MR:=unapply(diff(TR(x),x),x);
> MR(5);
> plot([MR(x),AR(x)],x=0..14);
> solve(MR(x)=0,x);
> TC:=x->x^3+x^2+x+1;
> plot(TC(x),x=0..4);
> PR:=x->TR(x)-TC(x);
> PR(5);
> plot(PR(x),x=0..4);
# Click on the last plot. Then click on the profit maximizing point to
# see approximately what the solution to the profit maximization problem
# is. Now plot TR, TC & PR in the same graph. Which curve is which? If
# you would like to add a title to your plot, put labels (names) on the
# axes or on the curves, ask your TA and s/he will show you how.
> plot([TR(x),TC(x),PR(x)],x=0..4);
> MC:=unapply(diff(TC(x),x),x);
> AC:=x->TC(x)/x;
> AC(5);
> plot([MC(x),AC(x)],x=0..4,y=0..100);
> plot([MR(x),AR(x),MC(x),AC(x)],x=0..4,y=0..100);
# Which curve is which in your last plot? Use the pointer to get an
# approximation to the optimal output choice and the price that the
# monopolist will charge for this output. Compare the values that you
# get to the actual values computed below.
> optx:=fsolve(MR(x)=MC(x),x);
> optp:=AR(optx);
> PRmax:=PR(optx);
> PRM:=x->PRmax;
> plot([PR(x),PRM(x)],x=0..4);
# For an example with a U-shaped MC curve you can use the TC curve 
# of the perfect competitor below. Execute the commands down to the
# plot(AC,MC) command. Then go back and reexecute the commands from
# plot(MR,AR,MC,AC) on to recalculate the optimal choices of the
# monopolist. The plots will look better if you change the x-scale to 
# 0,,6. If you
# then increase the 11 in TC to 21 and reexecute the MC and AC commands,
# 
# you will have an example where maximum profits are negative but still
# > PR(0). If you further 
# increase the 21 in TC to 31 you will have an example where MR < MC for
# all x and the monopolist will shut down.  Maple will not solve for
# optx because there is no
# optx.
# SUPPLY CURVE OF PERFECT COMPETITOR
> TC:=x->x^3-4*x^2+11*x+10;

                               3      2
                   TC := x -> x  - 4 x  + 11 x + 10

> plot(TC(x),x=0..4);
> MC:=unapply(diff(TC(x),x),x);

                                    2
                      MC := x -> 3 x  - 8 x + 11

> AC:=x->TC(x)/x;

                                      TC(x)
                           AC := x -> -----
                                        x

> plot({AC(x),MC(x)},x=0..4,y=0..50);
# Next determine the output which minimzes MC.
> MMC:=unapply(diff(MC(x),x),x);

                         MMC := x -> 6 x - 8

> xmin:=solve(MMC(x)=0,x);

                             xmin := 4/3

> MCmin:=MC(xmin);

                            MCmin := 17/3

> PR:=(p,x)->p*x-TC(x);

                     PR := (p, x) -> p x - TC(x)

> PR(MCmin,xmin);

                                 -334
                                 ----
                                  27

> PR(0,0);

                                 -10

# The supply curve does not extend to the minimum point on the MC
# curve.because the firm would make more money by producing nothing.
# The next commands determine the lowest price at which the firm would
# be willing to produce a positive output and also the lowest price at
# which maximum profits will be nonnegative. In the first case Maple
# finds more than one solution so the [] brackets are used to make the
# answer a list. The relevant part of the solution can then be
# identified as shown.
> x1:=[fsolve(PR(0,0)=PR(MC(x),x),x)];

                           x1 := [0, 0, 2.]

> p1:=MC(x1[3]);

                               p1 := 7.

> PR(p1,x1[3]);

                                 -10.

> x1min:=fsolve(MC(x)=AC(x),x);

                         x1min := 2.690647448

> p1min:=AC(x1min);

                         p1min := 11.19357148

> PR(p1min,x1min);

                                     -7
                               -.1 10

# The following commands plot the supply curve, the inverse supply curve
# and show where profits become nonnegative. Parametric plots are used
# so that the vertical section can be plotted. Change the axes to framed
# to see this section.and the dicontinuity in the supply curve.
> plot([[x,MC(x),x=x1[3]..4],[0,p,p=0..p1]]);

> plot([[MC(x),x,x=x1[3]..4],[p,0,p=0..p1]]);

> plot([[x,MC(x),x=x1[3]..4],[0,p,p=0..p1],[x,p1min,x=0..4]]);

# The firm's maximized profit function is PRmax(p)=PR(p,x(p)) where x(p)
# is the inverse supply curve. The last command plots this function.
> plot([[MC(x),PR(MC(x),x),x=x1[3]..4],[p,PR(0,0),p=0..p1]]);

> 
